
class Main extends egret.DisplayObjectContainer {


    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {
        this.stage.dirtyRegionPolicy = egret.DirtyRegionPolicy.ON;
        console.log("this.stage.dirtyRegionPolicy",this.stage.dirtyRegionPolicy)
        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin

            context.onUpdate = () => {

            }
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }

        this.runGame().catch(e => {
            console.log(e);
        })
    }

    private async runGame() {
        await this.loadResource()
        this.createGameScene();
        const result = await RES.getResAsync("description_json")
        //this.startAnimation(result);
        await platform.login();
        const userInfo = await platform.getUserInfo();
        console.log(userInfo);

    }

    private async loadResource() {
        try {
            const loadingView = new LoadingUI();
            this.stage.addChild(loadingView);
            await RES.loadConfig("resource/default.res.json", "resource/");
            await RES.loadGroup("preload", 0, loadingView);
           
            this.stage.removeChild(loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }

    private textfield: egret.TextField;
    private roleSp:egret.Sprite;
    private maskSp:egret.Sprite;
    /**
     * 创建游戏场景
     * Create a game scene
     */
    private createGameScene() {
        if(DEBUG)
        {
            console.log('debug......');
        }

        let sky = this.createBitmapByName("bg_jpg");
        this.addChild(sky);
        let stageW = this.stage.stageWidth;
        let stageH = this.stage.stageHeight;
        sky.width = stageW;
        sky.height = stageH;
       
       //模拟新手遮罩效果 ---开始
       var container:egret.Sprite = new egret.Sprite();
       
       this.maskSp = new egret.Sprite();       
       this.maskSp.graphics.beginFill(0xff0000,0.8);
       this.maskSp.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
       this.maskSp.graphics.endFill();
       container.addChild(this.maskSp);

       let sheet:egret.SpriteSheet = RES.getRes("myRoleAni_json");
       let bmp:egret.Bitmap = new egret.Bitmap(sheet.getTexture('attack0'));
       this.roleSp = new egret.Sprite();       
       this.roleSp.graphics.beginFill(0xff0000);
       this.roleSp.graphics.drawEllipse(0,0,200,100);
       this.roleSp.graphics.endFill();
       this.roleSp.blendMode = egret.BlendMode.ERASE;
       container.addChild(this.roleSp);
       this.roleSp.x = 90;
       this.roleSp.y = 340;


       var t:egret.RenderTexture = new egret.RenderTexture();
       t.drawToTexture(container);

       var btn:eui.Button = new eui.Button();
       btn.skinName
       this.addChild(btn);

       let guildBmp:egret.Bitmap = new egret.Bitmap();
       guildBmp.texture = t;
       this.addChild(guildBmp);
       guildBmp.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouch,this);
       guildBmp.pixelHitTest = true;
       //模拟新手遮罩效果 ---结束

    }
    private onTouch(evt:egret.TouchEvent):void{
        console.log("点击。。。。。。")
    }

    private testTween():void
    {
         let sheet:egret.SpriteSheet = RES.getRes("myRoleAni_json");
        //egret.log("====我的日志=====this.stage.dirtyRegionPolicy="+this.stage.dirtyRegionPolicy);
        //egret.log("====我的日志====="+sheet);
        callJsFunc("I am ts,i call u js!")
        let bmp:egret.Bitmap = new egret.Bitmap(sheet.getTexture('attack0'));
        
        this.roleSp = new egret.Sprite();
        this.addChild(this.roleSp);
        this.roleSp.addChild(bmp);
        
        let roleTw = egret.Tween.get(this.roleSp,{loop:true});
        roleTw.wait(1000);
        bmp.texture = sheet.getTexture('attack1');
        bmp.scaleX = bmp.scaleY = 1.2;
        roleTw.to({scaleX:0.5,scaleY:0.5,x:500,y:500},2000);
    }

    /**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
     */
    private createBitmapByName(name: string) {
        let result = new egret.Bitmap();
        let texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }

    /**
     * 描述文件加载成功，开始播放动画
     * Description file loading is successful, start to play the animation
     */
    private startAnimation(result: string[]) {

        

        let parser = new egret.HtmlTextParser();

        let textflowArr = result.map(text => parser.parse(text));
        let textfield = this.textfield;
        let count = -1;
        let change = () => {
            count++;
            if (count >= textflowArr.length) {
                count = 0;
            }
            let textFlow = textflowArr[count];

            // 切换描述内容
            // Switch to described content
            textfield.textFlow = textFlow;
            let tw = egret.Tween.get(textfield);
            tw.to({ "alpha": 1 }, 200);
            tw.wait(2000);
            tw.to({ "alpha": 0 }, 200);
            tw.call(change, this);

            
        };

        change();
    }
}